<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_Language
{
	/**
	 * List of supported languages in order of alphabet
	 * @var array
	 */
	public static $LANGUAGES = array(
		/**
		 * English (Built-in language)
		 */
		'en_US' => array(
			'englishName' => 'English',
			'localName'   => 'English',	
		),
		/**
		 * Vietnamese (Built-in language)
		 */
		'vi_VN' => array(
			'englishName' => 'Vietnamese',
			'localName'   => 'Tiếng Việt',	
		),
		/**
		 * German
		 */
		'de_DE' => array(
			'englishName' => 'German',
			'localName'   => 'Deutsch',	
		),
		/**
		 * Spanish
		 */
		'es_ES' => array(
			'englishName' => 'Spanish',
			'localName'   => 'Español',	
		),
		/**
		 * Polish
		 */
		'pl_PL' => array(
			'englishName' => 'Polish',
			'localName'   => 'Polski',	
		),
		/**
		 * Russian
		 */
		'ru_RU' => array(
			'englishName' => 'Russian',
			'localName'   => 'Русский',	
		),
		/**
		 * Chinese
		 */
		'zh_CN' => array(
			'englishName' => 'Chinese',
			'localName'   => '中文',	
		),
	);
	
	/**
	 * Get available languages
	 * 
	 * @return array
	 */
	public static function getAvailableLanguages()
	{
		$languages = array();
		
		/**
		 * Get the list of modules
		 */
		$modules 	= Gio_Core_File::getSubDir(ROOT_DIR . DS . 'modules');
		$numModules = count($modules);
		foreach ($modules as $module) {
			$moduleDir = ROOT_DIR . DS . 'modules' . DS . $module . DS . 'languages';
			if (!file_exists($moduleDir)) {
				continue;
			}
			
			/**
			 * Get list of language files in module
			 */
			$dirIterator = new DirectoryIterator($moduleDir);
			foreach ($dirIterator as $dir) {
				if (!$dir->isDot() && !$dir->isDir()) {
                	$file 	  = $dir->getFilename();
                	$sections = explode('.', $file);
                	$lang 	  = $sections[1];
                	$languages[$lang][] = $module;
            	}
			}
		}
		
		foreach ($languages as $lang => $modules) {
			if (count($modules) == $numModules) {
				if (isset(self::$LANGUAGES[$lang])) {
					$languages[$lang] = self::$LANGUAGES[$lang]; 
				} else {
					$languages[$lang] = array(
						'englishName' => $lang,
						'localName'   => $lang,
					);
				}
			} else {
				unset($languages[$lang]);
			}
		}
		
		return $languages;
	}
}